{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 🎯 CSPOT Helper Functions"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Export the results to `.csv`\n",
    "Once the CSPOT pipeline has been executed, all the output is stored within the csObject. An efficient way to export the results of csScore, cspotOutput, and the rescaled data is by using a function that saves them as a CSV file. This allows for easy sharing and analysis of the data in other programs."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# import packages\n",
    "import cspot as cs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# path to files needed for csExport\n",
    "projectDir = '/Users/aj/Documents/cspotExampleData'\n",
    "csObject = projectDir + '/CSPOT/csObject/exampleImage_cspotPredict.ome.h5ad'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Contents of the csObject have been exported to \"/Users/aj/Documents/cspotExampleData/CSPOT/csExport\"\n"
     ]
    }
   ],
   "source": [
    "cs.csExport(csObject,\n",
    "               projectDir,\n",
    "               fileName=None,\n",
    "               raw=False,\n",
    "               CellID='CellID',\n",
    "               verbose=True)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Same function if the user wants to run it via Command Line Interface**\n",
    "```\n",
    "python csExport.py \\\n",
    "            --csObject /Users/aj/Documents/cspotExampleData/CSPOT/cspotOutput/exampleImage_cspotPredict.ome.h5ad \\\n",
    "            --projectDir /Users/aj/Documents/cspotExampleData\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## We also provide a helper functions to vizualize the identified postive and negative cells for each marker."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The `addPredictions` function serves as a link between `cspot` and `scimap` package. It's useful for evaluating model performance. The function transforms results stored in `anndata.uns` to `anndata.obs` so they can be visualized using the `scimap` package's `sm.pl.image viewer` function. This displays `positive` and `negative` cells overlaid on the raw image.\n",
    "      \n",
    "The `addPredictions` function can take in two methods.  `cspotOutput` displays the result of running the `cspot` function,  while `csScore` shows the raw output produced by the `csScore`  function, which returns a probability score. The `midpoint` parameter,  with a default value of 0.5, can be adjusted to define what is considered a `positive` result, when method is set to `csScore`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Path to csObject\n",
    "csObject = projectDir + '/CSPOT/cspotOutput/exampleImage_cspotPredict.ome.h5ad'\n",
    "\n",
    "adata = cs.addPredictions (csObject, \n",
    "                    method='cspotOutput',\n",
    "                    cspotOutput='cspotOutput',\n",
    "                    csScore='csScore', \n",
    "                    midpoint=0.5)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['X_centroid', 'Y_centroid', 'Area', 'MajorAxisLength',\n",
       "       'MinorAxisLength', 'Eccentricity', 'Solidity', 'Extent', 'Orientation',\n",
       "       'CellID', 'imageid', 'p_ECAD', 'p_CD45', 'p_CD4', 'p_CD3D', 'p_CD8A',\n",
       "       'p_CD45R', 'p_KI67'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# check the results\n",
    "adata.obs.columns"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "As it can be seen the addition of `p_CD45, p_CD4, p_CD8A, p_CD45R, p_KI67, p_ECAD, p_CD3D` to `adata.obs`. These columns can be vizualized with `scimap`. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## We recommend creating a new environment to install scimap"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Download and install the scimap package. We recommend creating a new conda/python environment**\n",
    "\n",
    "```\n",
    "# create new conda env (assuming you have conda installed): executed in the conda command prompt or terminal\n",
    "conda create --name scimap -y python=3.8\n",
    "conda activate scimap\n",
    "\n",
    "```\n",
    "\n",
    "**Install `scimap` within the conda environment.**\n",
    "\n",
    "```\n",
    "pip install scimap\n",
    "\n",
    "# install jupyter notebook if you want to simply execute this notebook.\n",
    "pip install notebook\n",
    "\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Once `scimap` is installed the following function can be used to vizualize the results**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# import\n",
    "import scimap as sm\n",
    "import anndata as ad\n",
    "\n",
    "# import the csObject\n",
    "cwd = '/Users/aj/Desktop/cspotExampleData'\n",
    "csObject = cwd + '/CSPOT/cspotOutput/exampleImage_cspotPredict.ome.h5ad'\n",
    "adata = ad.read(csObject)\n",
    "\n",
    "# Path to the raw image\n",
    "image_path = '/Users/aj/Documents/cspotExampleData/image/exampleImage.tif'\n",
    "sm.image_viewer(image_path, adata, overlay='p_CD45')\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.16"
  },
  "vscode": {
   "interpreter": {
    "hash": "b155a34da96f173027cc83af6ba86a1d662c2b9e09ee27c56baf0fff8044d14a"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
